package bomb.menus;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import bomb.Game;
import bomb.utils.Arquivo;
import bomb.utils.Highscores;
import bomb.utils.Letras;

public class TelaScores extends BasicGameState {
    
    Image fundo = null;

    Image voltar = null;
    Image apagar = null;
    
    float voltar_scale = 1;
    float apagar_scale = 1;
    
    float scaleStep = 0.001f;
    

    int ident = 0;
    public TelaScores(int id) {
        this.ident = id;
    }
    
    

    @Override
    public int getID() {
        return ident;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        fundo = new Image("src/images/menufundo2.png");
        Image options = new Image("src/images/scoremenu.png");

        voltar = options.getSubImage(0, 0, 200, 50);
        apagar = options.getSubImage(0, 50, 200, 50);


    }
    @SuppressWarnings("deprecation")
    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        g.drawImage(fundo,0,0);
        Letras.getLetra1().drawString(10, 130, "Melhores do nível " + Integer.toString(Game.getFaseAtual()+1), Color.white);
        
        for(int k=0; k<9; k++)
        {
            Letras.getLetra1().drawString(50, 150+(20*(k+1)), "" + Integer.toString(k+1) + ". " + Integer.toString(Highscores.getScore(Game.getFaseAtual(), k)), Color.white);
           
        }
        voltar.draw(180,280,voltar_scale);
        apagar.draw(180,330,apagar_scale);
        
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException {

        
        Input input = gc.getInput();
        
        int mouseX = input.getMouseX();
        int mouseY = input.getMouseY();
       
        boolean insideVoltar = false;
        boolean insideApagar = false;
        
        if(input.isKeyPressed(Input.KEY_ESCAPE)) {
            gc.reinit();
            sbg.enterState(Game.MENUJOGOSTATE);
        }
        
        if( (mouseX >= 180 && mouseX < 380) && (mouseY >= 280 && mouseY < 330) ) {
            insideVoltar = true;
        }
        else if( (mouseX >= 180 && mouseX < 380) && (mouseY >= 330 && mouseY < 380) ) {
            insideApagar = true;
        }
        
        if(insideVoltar) {
              if(voltar_scale < 1.05f) {
                  voltar_scale += scaleStep * i;
              }
              if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                  Game.clickSound();
                  sbg.enterState(Game.MENUJOGOSTATE);
              }
        }
        else {
            if(voltar_scale > 1.0f) {
            voltar_scale -= scaleStep * i;
            } 
        } 
        
        if(insideApagar) {
              if(apagar_scale < 1.05f) {
                  apagar_scale += scaleStep * i;
              }
              if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                  Game.clickSound();
                  Highscores.clearScores(Game.getFaseAtual());
                  try {
                      Arquivo.gravar();
                  } catch (IOException ex) {
                      Logger.getLogger(LevelSelect.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
        }
        else {
            if(apagar_scale > 1.0f) {
            apagar_scale -= scaleStep * i;
            } 
        } 
        
    }
    
    
    
    
}